package top.market.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import top.market.model.dto.AddShoppingCartDTO;
import top.market.model.entity.Product;
import top.market.model.entity.ShoppingCart;
import top.market.model.vo.Result;
import top.market.model.vo.ShoppingCartVO;
import top.market.service.IProductService;
import top.market.service.IShoppingCartService;
import top.market.util.CurrentUserUtil;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author li yuhai
 * @since 2025-03-26
 */
@RestController
@RequestMapping("/shopping")
public class ShoppingCartController {

    @Autowired
    private IShoppingCartService shoppingCartService;

    @Autowired
    private IProductService productService;

   // 获取购物车的数量
    @GetMapping("/cart/account/number")
    public Result accountNumber() {
        Long id = CurrentUserUtil.getCustomUserDetails().getId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, id);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        long sum = list.stream().mapToLong(ShoppingCart::getNumber).sum();
        return Result.ok("", sum);
    }

    // 购物车商品列表
    @GetMapping("/cart/list")
    public Result list() {
        Long id = CurrentUserUtil.getCustomUserDetails().getId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, id);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        List<ShoppingCartVO> voList = list.stream().map(item -> {
            ShoppingCartVO vo = new ShoppingCartVO();
            BeanUtils.copyProperties(item, vo);
            return vo;
        }).collect(Collectors.toList());
        voList.forEach(item -> {
            Product product = productService.getById(item.getProductId());
            item.setProduct(product);
        });
        return Result.ok("", voList);
    }

    @PostMapping("/cart/add")
    public Result add(@RequestBody AddShoppingCartDTO data) {
        if (data == null) return Result.error("null");
        if (StrUtil.isBlank(data.getName()) || StrUtil.isBlank(data.getImage()) || data.getId() == null ){
            if (data == null) return Result.error("数据错误");
        }
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(data, shoppingCart);
        shoppingCart.setId(null);
        shoppingCart.setProductId(data.getId());
        shoppingCart.setUserId(CurrentUserUtil.getCustomUserDetails().getId());
        if (data.getNumber() == null || data.getNumber() <= 0) {
            shoppingCart.setNumber(1);
        }else  shoppingCart.setNumber(data.getNumber());
        // 查数据库的这个商品的物件, 设置价钱
        Product getById = productService.getById(shoppingCart.getProductId());
        if (getById == null) return Result.ok("没有改商品!");
        shoppingCart.setAmount(getById.getPrice().multiply(new BigDecimal(shoppingCart.getNumber())));
        shoppingCartService.save(shoppingCart);
        return Result.ok("添加成功");
    }

    // 更新购物车中的某件商品数量
    @PutMapping("/cart/number/update")
    public Result plus(@RequestBody AddShoppingCartDTO data) {
        if (data == null) return Result.error("null");
        if ( data.getId() == null || data.getId() <= 0  || data.getNumber() == null || data.getNumber() <= 0
            || data.getProductId() == null || data.getProductId() <= 0){
            if (data == null) return Result.error("数据错误");
        }
        // 查询出这个数据
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, CurrentUserUtil.getCustomUserDetails().getId());
        wrapper.eq(ShoppingCart::getId, data.getId()); // 购物车id
        wrapper.eq(ShoppingCart::getProductId, data.getProductId());
        ShoppingCart shoppingCart = shoppingCartService.getOne(wrapper);
        // 更新
        Product product = productService.getById(shoppingCart.getProductId());
        shoppingCart.setNumber(data.getNumber());
        shoppingCart.setAmount(product.getPrice().multiply(new BigDecimal(shoppingCart.getNumber())));
        shoppingCartService.updateById(shoppingCart);
        return Result.ok("添加成功");
    }
}
